<template>
  <div class="touchmove">
    <!-- <v-touch
      v-on:swipeleft="onSwipeLeft"
      v-on:swiperight="onSwipeRight"
      tag="div"
    > -->
    <router-view></router-view>
    <!-- </v-touch> -->
    <Nav class="nav" />
  </div>
</template>
<script>
import Nav from "./components/Nav.vue";
export default {
  components: {
    Nav,
  },
  data() {
    return {};
  },
  created() {
    this.getLocation();
  },
  mounted() {
    // document.body.addEventListener(
    //   "touchmove",
    //   function (e) {
    //     e.preventDefault(); // 阻止默认的处理方式(阻止下拉滑动的效果)
    //   },
    //   { passive: false }
    // ); // passive 参数不能省略，用来兼容ios和android
  },
  methods: {
    // onSwipeLeft() {
    //   this.$router.go(1);
    // },
    // onSwipeRight() {
    //   this.$router.go(-1);
    // },
    getLocation() {
      const self = this;
      AMap.plugin("AMap.Geolocation", function () {
        var geolocation = new AMap.Geolocation({
          // 是否使用高精度定位，默认：true
          enableHighAccuracy: true,
          // 设置定位超时时间，默认：无穷大
          timeout: 10000,
        });

        geolocation.getCurrentPosition();
        AMap.event.addListener(geolocation, "complete", onComplete);
        AMap.event.addListener(geolocation, "error", onError);

        function onComplete(data) {
          // data是具体的定位信息  精准定位
          // console.log(data, "onComplete");
          self.$store.dispatch("setLocation", data);
          self.$store.dispatch("setAddress", data.formattedAddress);
        }

        function onError(data) {
          // 定位出错  非精准定位
          // console.log(data,"onError")
          self.getLngLatLocation();
        }
      });
    },
    getLngLatLocation() {
      const self = this;
      AMap.plugin("AMap.CitySearch", function () {
        var citySearch = new AMap.CitySearch();
        citySearch.getLocalCity(function (status, result) {
          if (status === "complete" && result.info === "OK") {
            // 查询成功，result即为当前所在城市信息
            // console.log(result);
            AMap.plugin("AMap.Geocoder", function () {
              var geocoder = new AMap.Geocoder({
                // city 指定进行编码查询的城市，支持传入城市名、adcode 和 citycode
                city: result.adcode,
              });

              var lnglat = result.rectangle.split(";")[0].split(",");

              geocoder.getAddress(lnglat, function (status, data) {
                if (status === "complete" && data.info === "OK") {
                  // result为对应的地理位置详细信息
                  // console.log(data, "非精确定位");
                  self.$store.dispatch("setLocation", {
                    addressComponent: {
                      city: result.city,
                      province: result.province,
                    },
                    formattedAddress: data.regeocode.formattedAddress,
                  });
                  self.$store.dispatch(
                    "setAddress",
                    data.regeocode.formattedAddress
                  );
                }
              });
            });
          }
        });
      });
    },
  },
};
</script>
<style>
.icon {
  width: 1em;
  height: 1em;
  vertical-align: -0.15em;
  fill: currentColor;
  overflow: hidden;
}
* {
  margin: 0;
  padding: 0;
}
li {
  list-style: none;
}
a {
  text-decoration: none;
}
.same {
  width: 95vw;
  margin: 0 auto;
}
::-webkit-scrollbar {
  display: none;
}
</style>
<style scoped>
.nav {
  position: fixed;
  bottom: 0;
}
</style>
